/* 
 * Copyright (C) 2009 Jeffrey Rusterholz
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// Acaletto - Quicky add an entry in Google calendar.
// Author: Jeffrey Rusterholz
// Date:   13 August 2009

package nl.jalava.acalleto;

import java.util.GregorianCalendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TimePicker;
import android.widget.Toast;

public class AcalettoActivity extends Activity {
	
	// Preferences.
	public static final String PREFS_NAME = "AcalettoPrefs";
	
	// Menu.
	private static final int MENU_SETTINGS = Menu.FIRST; 
	private static final int MENU_ABOUT = Menu.FIRST + 1;
	
	// Dialogs.
	private static final int ABOUT_DIALOG = 1;
	
	EditText AcEditText;
	Button OkButton, CloseButton, ButtonMon;
	Spinner SpinnerDays, SpinnerWeeks;
	TimePicker tp;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        AcEditText = (EditText) findViewById(R.id.EditText);
        OkButton = (Button) findViewById(R.id.OkButton);
        CloseButton = (Button) findViewById(R.id.CloseButton);
                
        tp = (TimePicker)findViewById(R.id.TimePicker01);
        tp.setIs24HourView(true);
        
        SpinnerDays = (Spinner) findViewById(R.id.SpinnerDays);

        GregorianCalendar cal = new GregorianCalendar();
    	int today = cal.get(GregorianCalendar.DAY_OF_WEEK);
    	if (today == 1) { today = 8; } // Sunday.
        SpinnerDays.setSelection(today - 2); // Values are 0 to 6.

        SpinnerWeeks = (Spinner) findViewById(R.id.SpinnerWeeks);

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);       
        GoogleCalendar.setLogin(prefs.getString("username", ""),  prefs.getString("password", ""));
		try {
			GoogleCalendar.authenticate(false);
		} catch (Exception e1) {			
			Toast toast = Toast.makeText(getBaseContext(), 
					e1.getMessage(), Toast.LENGTH_SHORT);
			toast.show();
		}
		
		String xml;
		try {
			xml = GoogleCalendar.ownCalendars();
		} catch (Exception e1) {
			xml = "No Calendars!\n" + e1.getMessage();
		}
		//Toast toast = Toast.makeText(getBaseContext(), xml,Toast.LENGTH_LONG);
		//toast.show();
		
        OkButton.setOnClickListener(new OnClickListener() {
        	public void onClick(View v) {    
        		if (AcEditText.getText().length() == 0) {
        			Toast toast = Toast.makeText(getBaseContext(), getResources().getString(R.string.No_Text), Toast.LENGTH_SHORT);
        			toast.show();
        			return;
        		}
        		
        		GregorianCalendar cal = new GregorianCalendar();
        		int week = SpinnerWeeks.getSelectedItemPosition();
        		int today = cal.get(GregorianCalendar.DAY_OF_WEEK);
        		if (today == 1) { today = 8; }
        		today -= 2;
        		int day = SpinnerDays.getSelectedItemPosition() - today;
        		cal.add(GregorianCalendar.DAY_OF_YEAR, week * 7 + day);
				/*Toast toast = Toast.makeText(getBaseContext(), 
						AcEditText.getText() + " on " +
						(cal.get(GregorianCalendar.MONTH) + 1) + "/" + 
						cal.get(GregorianCalendar.DAY_OF_MONTH) + "/" +
						cal.get(GregorianCalendar.YEAR) + " at " +
						tp.getCurrentHour() + ":" + tp.getCurrentMinute(),
						Toast.LENGTH_LONG);*/
        		Toast toast = Toast.makeText(getBaseContext(), getResources().getString(R.string.Item_sent), Toast.LENGTH_SHORT);
				try {
					if (GoogleCalendar.createEvent(AcEditText.getText().toString(), 
							cal.get(GregorianCalendar.YEAR), 
							cal.get(GregorianCalendar.MONTH),
							cal.get(GregorianCalendar.DAY_OF_MONTH),
							(int) tp.getCurrentHour(),
							(int) tp.getCurrentMinute())) {
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					toast = Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG);
					
				}
				toast.show();				
			}
		});
        
        CloseButton.setOnClickListener(new OnClickListener() {			
			public void onClick(View v) {
				finish();			
			}
		});
        
    }

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {		
		menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
			.setIcon(android.R.drawable.ic_menu_manage)
			.setAlphabeticShortcut((char) R.string.menu_settings_shortcut);
		menu.add(0, MENU_ABOUT, 0, R.string.menu_about)
			.setIcon(android.R.drawable.ic_menu_info_details);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) { 
		case MENU_SETTINGS: 
			startActivity(new Intent(this, Preferences.class));
			break;
		case MENU_ABOUT: 
			showDialog(ABOUT_DIALOG);
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) { 
		case ABOUT_DIALOG: 
			View AboutView = getLayoutInflater().inflate(R.layout.about, null);
        	return new AlertDialog.Builder(this)
        		.setView(AboutView)
        		.setNeutralButton(android.R.string.ok, null).create();
		}
		return super.onCreateDialog(id);
	}
}